﻿using DevExpress.XtraTab;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ApartmanYonetim.Siniflar
{
    class Fonksiyon
    {
        Veritabani vt = new Veritabani();

        public void TabOlustur(Form form)
        {

            AnaForm afrm = (AnaForm)Application.OpenForms["AnaForm"];
            List<string> dizi = new List<string>();
            foreach (XtraTabPage item in afrm.tabControl.TabPages)
            {
                dizi.Add(item.Text);
            }

            if (!dizi.Contains(form.Text))
            {
                XtraTabPage page = new XtraTabPage();
                form.Visible = true;
                form.Dock = DockStyle.Fill;
                form.FormBorderStyle = FormBorderStyle.None;
                form.TopLevel = false;
                page.Text = form.Text;
                page.Controls.Add(form);
                afrm.tabControl.TabPages.Add(page);
                afrm.tabControl.SelectedTabPage = page;
            }

            
        }


        public void TabKapat()
        {
            AnaForm afrm = (AnaForm)Application.OpenForms["AnaForm"];
            XtraTabControl tabcontrol = afrm.tabControl;
            tabcontrol.TabPages.RemoveAt(tabcontrol.SelectedTabPageIndex);
        }

        public void TabKapat(Form form)
        {
            AnaForm afrm = (AnaForm)Application.OpenForms["AnaForm"];
            XtraTabControl tabcontrol = afrm.tabControl;
            foreach (XtraTabPage item in tabcontrol.TabPages)
            {
                if (form.Text == item.Text)
                {
                    tabcontrol.TabPages.Remove(item);
                }
            }
        }


        public void Listele(string sorgu, DataGridView datagrid)
        {
            DataTable dt = vt.GetDataTable(sorgu);
            datagrid.ReadOnly = true;
            datagrid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            datagrid.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            datagrid.RowHeadersVisible = false;
            datagrid.MultiSelect = false;
            datagrid.DataSource = dt;
            datagrid.Columns["ID"].Visible = false;
            
        }

        public void ComboboxDoldur(string sorgu, string value, string text, ComboBox comboBox)
        {
            DataTable dt = vt.GetDataTable(sorgu);

            DataRow dr = dt.NewRow();
            dr[value] = "0";
            dr[text] = "Seçiniz..";
            dt.Rows.InsertAt(dr, 0);

            comboBox.ValueMember = value;
            comboBox.DisplayMember = text;
            comboBox.DataSource = dt;
            comboBox.SelectedIndex = 0;
        }

        public void ComboboxDoldur(Type enumtipi, ComboBox combobox)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("ID",typeof(int));
            dt.Columns.Add("text",typeof(string));

            foreach (string name in Enum.GetNames(enumtipi))
            {
                dt.Rows.Add(name.Replace('_', ' '), Enum.Parse(enumtipi, name));
            }
            

            combobox.ValueMember = "ID";
            combobox.DisplayMember = "text";
            combobox.DataSource = dt;
            
        }

      
    }
}
